﻿// //------------------------------------------------------------------------------
// // <copyright file="" company="Dascoba Development">
// //  Copyright © Dascoba Development 2010
// // </copyright>
// //------------------------------------------------------------------------------
using System;

namespace Dascoba.Umb.FileManager.Dialogs
{
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;

    using Dascoba.Umb.FileManager.Support;

    using umbraco;
    using umbraco.BasePages;
    using umbraco.cms.businesslogic.media;

    using Media = umbraco.cms.businesslogic.media.Media;

    /// <summary>
    ///   Upload File
    /// </summary>
    public partial class ImportMedia : UmbracoEnsuredPage
    {
        /// <summary>
        ///   Handles the Load event of the Page control.
        /// </summary>
        /// <param name = "sender">The source of the event.</param>
        /// <param name = "e">The <see cref = "System.EventArgs" /> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            this.pane_importmediaitem.Text = ui.Text(
                "fileman", "selectMediaTypeLocation", this.Request["item"], CurrentUser);

            if (this.IsPostBack)
            {
                return;
            }

            this.PopulateDropDown();
        }

        /// <summary>
        ///   Populates the drop down.
        /// </summary>
        private void PopulateDropDown()
        {
            var mediaTypes = MediaType.GetAll;

            this.DropDownList1.Items.Clear();
            foreach (var mediaType in mediaTypes.Where(mediaType => mediaType.Alias.ToLower() != "folder"))
            {
                this.DropDownList1.Items.Add(mediaType.Alias);
            }
        }

        /// <summary>
        ///   Handles the Click event of the btnImport control.
        /// </summary>
        /// <param name = "sender">The source of the event.</param>
        /// <param name = "e">The <see cref = "System.EventArgs" /> instance containing the event data.</param>
        protected void BtnImportClick(object sender, EventArgs e)
        {
            var itemUrl = this.Request["item"];
            var type = this.Request["type"];
            var parentId = !String.IsNullOrEmpty(this.ContentPicker1.Text) ? int.Parse(this.ContentPicker1.Text) : -1;
            var mediaType = this.DropDownList1.SelectedValue;

            var itemName = "Item";
            var mediaImported = false;
            Media singleMedia = null;
            List<Media> multipleMedia = null;

            switch (type)
            {
                case "file":
                    {
                        itemName = Path.GetFileName(itemUrl);
                        singleMedia = Support.Media.ImportFileMedia(itemUrl, parentId, mediaType, false);

                        mediaImported = singleMedia != null;
                    }
                    break;
                case "folder":
                    {
                        itemName = Path.GetDirectoryName(itemUrl).Replace("\\", "");
                        singleMedia = Support.Media.ImportFileMedia(itemUrl, parentId, "Folder", false);

                        if (singleMedia != null)
                        {
                            multipleMedia = Support.Media.ImportFolderMedia(itemUrl, singleMedia.Id, mediaType, false);
                            mediaImported = multipleMedia != null;
                        }
                    }
                    break;
            }

            if (mediaImported)
            {
                switch (type)
                {
                    case "file":
                        this.pane_status.Text = ui.Text("fileman", "fileImported");
                        this.linkPreview.NavigateUrl = (string)singleMedia.getProperty("umbracoFile").Value;
                        this.linkPreview.Visible = true;
                        break;
                    case "folder":
                        if (multipleMedia != null)
                        {
                            this.pane_status.Text = ui.Text(
                                "fileman", "folderImported", multipleMedia.Count.ToString(), CurrentUser);
                        }
                        break;
                }
                this.litStatus.Text = ui.Text("fileman", "importSuccess", itemName, CurrentUser);
                this.imgStatus.ImageUrl = Constants.SuccessIcon;
            }
            else
            {
                this.pane_status.Text = ui.Text("fileman", "importFailed");
                this.litStatus.Text = ui.Text("fileman", "importFail", itemName, CurrentUser);
                this.imgStatus.ImageUrl = Constants.FailureIcon;
            }

            this.pane_importmediaitem.Visible = false;
            this.pane_status.Visible = true;
        }
    }
}